﻿using System.Windows.Forms;
using StockLib;
using StockUI.Properties;
using System;

namespace StockUI
{
    public partial class DialogStockBlockPut : DialogAbstract
    {
        public Stock Stock { get; set; }

        public DialogStockBlockPut()
        {
            InitializeComponent();

            imageList.Images.Add(Resources.folder);

            listBlock.PaintItem = delegate(ListViewItem item, object data)
            {
                StockBlock block = data as StockBlock;

                item.Text = string.Format("{0} ({1})", block.Name, block.StockNumber);
                item.Tag = block;
                item.ImageIndex = 0;
            };

            StockBlockGroupCustom.Instance.DataChanged += new StockBlockGroupDataEventHandler(StockBlockGroupCustom_DataChanged);
        }

        private void StockBlockGroupCustom_DataChanged(object sender, EventArgs e)
        {
            listBlock.Bind();
        }

        private void DialogStockPutBlock_Load(object sender, System.EventArgs e)
        {
            if (Stock == null)
            {
                Stock = StockContainer.Instance.CurrentStock;
            }
            if (Stock == null)
            {

                throw new Exception("Stock对象为空");
            }

            this.Text += " - " + Stock.Name;

            listBlock.DataSource = StockBlockGroupCustom.Instance.Values;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!CheckBlockSelected())
                return;

            StockBlock block = listBlock.SelectedItems[0].Tag as StockBlock;
            if (StockBlockGroupCustom.Instance.ContainsKey(block.BlockId))
            {
                if (!block.ContainsKey(Stock.CodeKey))
                {
                    block[Stock.CodeKey] = Stock;
                    block.StockNumber = block.Count;
                    StockBlockGroupCustom.Instance.RaiseStockDataChanged(sender, e);
                }
            }

            Close();
        }

        private void listBlock_DoubleClick(object sender, EventArgs e)
        {
            btnOk_Click(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DialogStockBlockAdd dialog = new DialogStockBlockAdd();
            dialog.ShowDialog();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!CheckBlockSelected())
                return;

            StockBlock domain = listBlock.SelectedItems[0].Tag as StockBlock;
            if (domain.BlockId == StockBlockGroupCustom.Instance.ZXGBlock.BlockId)
            {
                MessageBox.Show(this, "不能删除自选股！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (MessageBox.Show(this, "确定删除该版块吗？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (StockBlockGroupCustom.Instance.ContainsKey(domain.BlockId))
                {
                    StockBlockGroupCustom.Instance.Remove(domain.BlockId);
                    StockBlockGroupCustom.Instance.RaiseDataChanged(sender, e);
                }
            }
        }

        private void btnRename_Click(object sender, EventArgs e)
        {
            if (!CheckBlockSelected())
                return;

            StockBlock block = listBlock.SelectedItems[0].Tag as StockBlock;
            if (block.BlockId == StockBlockGroupCustom.Instance.ZXGBlock.BlockId)
            {
                MessageBox.Show(this, "不能对自选股重命名！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DialogStockBlockRename dialog = new DialogStockBlockRename();
            dialog.StockBlock = block;
            dialog.ShowDialog();
        }

        private bool CheckBlockSelected()
        {
            if (listBlock.SelectedItems.Count <= 0)
            {
                MessageBox.Show(this, "请选择版块", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            return true;
        }
    }
}
